var city = require('../../../utils/cityList.js');
var getCityAll = require('../../../utils/getCity.js');

Page({
  data: {
    //城市下拉
    defaultCity: '定位中',
    latitude: "",
    longitude: "",
    citycode: "",
    cityData: {},
    hotCityData: [],
    _py: ["hot", "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "W", "X", "Y", "Z"],
    hidden: true,
    showPy: '★',
    backurl: "",
    tipCity: "",
    showTip: ""
  },
  onLoad: function () {
    const that = this;
    wx.getStorage({
      key: "backroute",
      success: function (res) {
        that.setData({
          backurl: res.data
        })
      }
    });
    getCityAll.getSingleCity.then(function (city) {
      that.setData({
        defaultCity: city.fullname,
        latitude: city.lat,
        longitude: city.lng,
        citycode: city.citycode
      })
    })
    that.setData({
      cityData: city.all,
      hotCityData: city.hot
    });
  },

  //选择城市
  selectCity: function (e) {
    const that = this;
    var dataset = e.currentTarget.dataset;
    console.log(dataset)
    wx.clearStorage()
    wx.setStorage({
      key: "selectCity",
      data: JSON.stringify(dataset)
    })
    that.setData({
      tipCity: dataset.fullname,
      showTip: "showtip"
    })
    setTimeout(function () {
      wx.switchTab({
        url: that.data.backurl
      })
    }, 500)
  },
  touchstart: function (e) {
    this.setData({
      index: e.currentTarget.dataset.index,
      Mstart: e.changedTouches[0].pageX
    });
  },
  touchmove: function (e) {
    var history = this.data.historyList;
    var move = this.data.Mstart - e.changedTouches[0].pageX;
    history[this.data.index].x = move > 0 ? -move : 0;
    this.setData({
      historyList: history
    });
  },
  touchend: function (e) {
    var history = this.data.historyList;
    var move = this.data.Mstart - e.changedTouches[0].pageX;
    history[this.data.index].x = move > 100 ? -180 : 0;
    this.setData({
      historyList: history
    });
  },
  //获取文字信息
  getPy: function (e) {
    this.setData({
      hidden: false,
      showPy: e.target.id,
    })
  },

  setPy: function (e) {
    this.setData({
      hidden: true,
      scrollTopId: this.data.showPy
    })
  },

  //滑动选择城市
  tMove: function (e) {
    var y = e.touches[0].clientY,
      offsettop = e.currentTarget.offsetTop;

    //判断选择区域,只有在选择区才会生效
    if (y > offsettop) {
      var num = parseInt((y - offsettop) / 12);
      this.setData({
        showPy: this.data._py[num]
      })
    };
  },

  //触发全部开始选择
  tStart: function () {
    this.setData({
      hidden: false
    })
  },

  //触发结束选择
  tEnd: function () {
    this.setData({
      hidden: true,
      scrollTopId: this.data.showPy
    })
  }
})